package com.sciencep.pro.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sciencep.pro.common.config.Constant;
import com.sciencep.pro.core.dao.BranchMapper;
import com.sciencep.pro.core.domain.dto.project.BranchContactTO;
import com.sciencep.pro.core.domain.dto.project.BranchTO;
import com.sciencep.pro.core.domain.entity.Branch;
import com.sciencep.pro.core.domain.entity.BranchContact;
import com.sciencep.pro.core.service.IBranchContactService;
import com.sciencep.pro.core.service.IBranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sciencep
 */
@Service
public class BranchServiceImpl extends ServiceImpl<BranchMapper, Branch> implements IBranchService {


    @Autowired
    IBranchContactService branchContactService;

    @Override
    public List<BranchTO> getBranchList() {
        List<Branch> branches = this.list(new LambdaQueryWrapper<Branch>().eq(Branch::getIsDel, Constant.IsDelete.NO));
        if (CollUtil.isEmpty(branches)) {
            return new ArrayList<>();
        }
        List<BranchTO> result = new ArrayList<>();
        branches.forEach(o ->
                result.add(new BranchTO().setBranchName(o.getDataName()).setBranchId(String.valueOf(o.getId())))
        );

        return result;
    }

    @Override
    public List<BranchContactTO> getBranchContactTO(List<String> branchIds) {

        if (CollUtil.isEmpty(branchIds)) {
            return new ArrayList<>();
        }
        List<BranchContact> branchContacts = branchContactService.list(new LambdaQueryWrapper<BranchContact>().in(CollUtil.isNotEmpty(branchIds), BranchContact::getBranchID, branchIds).eq(BranchContact::getIsDel, Constant.IsDelete.NO));
        if (CollUtil.isEmpty(branchContacts)) {
            return new ArrayList<>();
        }
        List<BranchContactTO> result = new ArrayList<>();
        branchContacts.forEach(o ->
                result.add(new BranchContactTO().setContactId(o.getContactId()).setContactName(String.valueOf(o.getContactName())))
        );

        return result;
    }
}
